package com.atguigu.web;

import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.utils.WebUtils;
import com.google.code.kaptcha.Constants;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(value = "/userServlet")
public class UserServlet extends BaseServlet {

    UserService userService = new UserServiceImpl();

    protected void ajaxExistsUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1 获取用户名
        String username = req.getParameter("username");
        //2 调用userService.existsUsername(username);
        boolean existsUsername = userService.existsUsername(username);
        Map<String,Object> map = new HashMap<>();
        map.put("existsUsername",existsUsername);
        Gson gson = new Gson();
        String s = gson.toJson(map);
        resp.getWriter().write(s);
    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1 销毁Session
        req.getSession().invalidate();
        // 2 重定向到首页
        resp.sendRedirect(req.getContextPath());
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //        1 获取请求的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //        2 调用userService.login(  ) : User
        User loginUser = userService.login(new User(null, username, password, null));
        //        3 根据login()的返回值决定是否登录成功
        if (loginUser == null) {
            //                登录失败
            request.setAttribute("msg","用户名或密码错误");
            request.setAttribute("username",username);
            //        login.jsp
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        } else {
            //                登录成功
            request.getSession().setAttribute("user", loginUser);
            //        login_success.jsp
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request,response);
        }
    }


    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取Session域中的验证码
        String token = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        // 删除Session域中的验证码
        request.getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);


        //        1  获取请求的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");

        User user = WebUtils.copyParamToBean(new User(), request.getParameterMap());

//        2 检查验证码是否正确
        if (token != null && token.equalsIgnoreCase(code)) {
            //                正确
//        3 检查用户名是否可用

            if (userService.existsUsername(username)) {
                //        不可用
                //                跳回注册页面
                System.out.println("用户名已存在!");

                request.setAttribute("msg", "用户名已存在!");
                request.setAttribute("username", username);
                request.setAttribute("email", email);

//                跳回注册页面
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            } else {
                //                可用
                //        保存到数据库
                userService.registUser(new User(null, username, password, email));
                //                跳到注册成功页面
                request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request, response);
            }
        } else {
//        不正确
            System.out.println("验证码不正确");
            request.setAttribute("msg", "验证码错误");
            request.setAttribute("username", username);
            request.setAttribute("email", email);
//                跳回注册页面
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }
    }






}
